<?php

defined('_JEXEC') or die;

class ExercisesViewExercises extends JViewLegacy {

    protected $categories;

    public function display($tpl = null) {
        $this->categories = $this->get('Category');
        $model = $this->getModel();
        $categoryid = JRequest::getVar('cate');
        foreach ($this->categories as $category){
            $categoryids[] = $category->id; 
        }
        if (in_array($categoryid, $categoryids)) {
            jimport('joomla.application.component.view');
            $this->exercise = $model->getExercisesByCateId($categoryid);
            $this->setLayout('exercise');
        }
        if (count($errors = $this->get('Errors'))) {
            JError::raiseError(500, implode('<br />', $errors));
            return false;
        }
        $this->prepareDocument();
        $document = JFactory::getDocument();
        $document->addStyleSheet(COM_EXERCISES_URL . 'assets/css/screen.css');
        $document->addScript(COM_EXERCISES_URL . 'assets/js/lib.js');
        $document->addScript(COM_EXERCISES_URL . 'assets/js/common.js');
        parent::display($tpl);
    }

    /**
     * Prepares the document.
     *
     * @since	1.6
     */
    protected function prepareDocument() {
        $app = JFactory::getApplication();
        $document = JFactory::getDocument();
        $title = $document->getTitle();
        $title = JText::_('COM_EXERCISES_EXERCISES_VIEW_DEFAULT_TITLE') . ' | ' . $title;
        $document->setTitle($title);
    }


}
